// subpages0/profitCenter/profitCenter.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTip:true,
    showLoading: true,
    noMoretip: false,
    page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    if (e && e.title) {
      that.setData({
        title: e.title
      })
    }
    if (that.data.title) {
      app.setNavtitle(that.data.title);
    } else {
      app.setNavtitle("收益中心");
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.getcurDate();
    }
  },
  toincomeWithdraw: function (e) {// 申请提现
    wx.navigateTo({
      url: '/pages/incomeWithdraw/incomeWithdraw'
    })
  },
  getcurDate: function () {
    var that = this;
    var myDate = new Date();
    var curyear = myDate.getFullYear();   //获取完整的年份(4位,1970-????)  
    var curmonth = myDate.getMonth() + 1;      //获取当前月份(0-11,0代表1月)  
    var curday = myDate.getDate();       //获取当前日(1-31)  
    var curDate = curyear + '-' + curmonth;
    var showDate = curyear + '年' + curmonth + '月';
    this.setData({
      date: curDate,
      maxDate: curDate,
      showDate: showDate,
    })
    that.requestProfitrecord();
  },
  hideTip:function(){
    var that = this;
    that.setData({
      showTip:false
    })
  },
  bindDateChange:function(e){
    var that = this;
    var date = e.detail.value;
    var dateArr = date.split('-');
    var showDate = dateArr[0] + '年' + dateArr[1] + '月';
    that.setData({
      date:date,
      showDate: showDate
    })
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestProfitrecord();
  },
  requestProfitrecord: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_deduct_goods_profit';
    data.month = that.data.date ? that.data.date:'';
    data.page = page;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.recordsList ? that.data.recordsList : '[]';
          var curArr = res.data.data.list;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data.list;
          }
          that.setData({
            recordsList: allArr,
            totalProfit: res.data.data.totalProfit,
            todayProfit: res.data.data.todayProfit
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.recordsList);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              recordsList: []
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.requestProfitrecord();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestProfitrecord();
    }
  },
})